﻿using System;
using System.Diagnostics;
using LightWeb.DomainModel;
using LightWeb.Respository;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NUnit.Framework;

namespace LightWeb.RespositoryTest
{
    [TestFixture]
    public class CreateDbSchemaFixture
    {
        private Configuration _cfg;

        [SetUp]
        public void SetContext()
        {
            HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
            _cfg = new Configuration();
            _cfg.Configure();
        }
        [Test]
        public void CreateTableSchema()
        {
            Console.WriteLine(_cfg.Properties);
            SchemaExport export = new SchemaExport(_cfg);
            export.Execute(true, true, false);
        }
        [Test]
        public void DropTest()
        {
            var export = new SchemaExport(_cfg);
            export.Drop(true, true);
        }


    }

    public class SchemaUpdateFixture
    {
        private Configuration _cfg;

        [SetUp]
        public void SetContext()
        {
            HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
            _cfg = new Configuration();
            _cfg.Configure();

        }

        [Test]
        public void UpdateExistingDatabaseSchemaTest()
        {

            var update = new SchemaUpdate(_cfg);
            update.Execute(true, true);

        }

        [Test]
        public void ModelValidator()
        {
            var val = new SchemaValidator(_cfg);
            val.Validate();

        }
    }

    public class DbConnectionFixture
    {
        public void Test()
        {
            HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
            DataContext.Init();


            Stopwatch sw = new Stopwatch();
            sw.Start();
            try
            {
                using (OurTransactionScope ts = new OurTransactionScope())
                {
                    var context = DataContext.Current;
                    for (int i = 0; i < 100; i++)
                    {
                        context.Session.Save(new ArticalInfo()
                         {
                             Title = "LightWeb Blog #" + i + " Artical",
                             Summary = "Artical SummaryArtical SummaryArtical SummaryArtical SummaryArtical SummaryArtical SummaryArtical SummaryArtical SummaryArtical "
                             + "SummaryArtical SummaryArtical SummaryArtical SummaryArtical SummaryArtical SummaryArtical SummaryArtical SummaryArtical SummaryArtical "
                             + "SummaryArtical SummaryArtical SummaryArtical SummaryArtical SummaryArtical SummaryArtical SummaryArtical SummaryArtical SummaryArtical "
                             + "SummaryArtical SummaryArtical Summary",
                             PostDate = DateTime.Now,
                             UpdateDate = DateTime.Now
                         });

                        for (int j = 0; j < 5; j++)
                        {
                            context.Session.Save(new ArticalComment()
                            {
                                ArticalId = i,
                                Content = "Comment Content",
                                Date = DateTime.Now,
                                Email = "flyear.cheng@gmail.com",
                                Name = "Flyear",
                                Site = "http://flyear.cnblogs.com"
                            });
                        }
                    }

                    ts.Complete();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            sw.Stop();
            Console.WriteLine(sw.ElapsedMilliseconds);
        }
    }
}
